package ro.fortech.peaa.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import ro.fortech.peaa.domain.Job;
import ro.fortech.peaa.service.api.IJobManager;

/**
 * Controller for the job list page. 
 * 
 * @author alexandrum
 *
 */
public class JobsController extends AbstractController {
	
	/** The job manager. */
	private IJobManager jobManager;
	
	/**
	 * Sets a new value to the jobManager.
	 * 
	 * @param jobManager The new value to set
	 */
	public void setJobManager(IJobManager jobManager) {
		this.jobManager = jobManager;
	}	
	
	/**
	 * Handles the request to show the job list.
	 * 
	 * @param request The HTTP request
	 * @param response The HTTP response
	 * @return A ModelAndView containing all the jobs
	 */
	protected ModelAndView handleRequestInternal(HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		logger.info("STARTED");
		
		Map<String, Object> myModel = new HashMap<String, Object>();
		List<Job> jobs = this.jobManager.findAll();
		logger.debug("Found " + jobs.size() + " jobs.");
		myModel.put("jobs", jobs);
		
		logger.info("ENDED");
		return new ModelAndView("jobs", myModel);
	}
}
